#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 震动传感器SW-420

import RPi.GPIO as GPIO
import time

pin = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)

def swfuntion(lastTime, lastInput, lastIndex):
    thisTime = time.time()
    thisInput = GPIO.input(pin)
    if ((thisTime - lastTime) > 0.5) and (thisInput == 1):
        print(str(lastIndex + 1) + "..." + str(lastInput) + "----" + str(thisTime - lastTime) + "-->" + str(thisInput))
        return thisTime, thisInput, (lastIndex + 1)
    else:
        return lastTime, lastInput, lastIndex

try:
    itemTime = time.time()
    itemInput = GPIO.input(pin)
    itemIndex = 0
    for i in range(1, 3100):
        res0, res1, res2 = swfuntion(itemTime, itemInput, itemIndex)
        itemTime = res0
        itemInput = res1
        itemIndex = res2
        time.sleep(0.1)
except KeyboardInterrupt:
    GPIO.cleanup()